package com.forgeessentials.multiworld.gen;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import net.minecraft.init.Biomes;
import net.minecraft.util.WeightedRandom;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.gen.layer.GenLayer;
import net.minecraft.world.gen.layer.IntCache;
import net.minecraftforge.common.BiomeManager;
import net.minecraftforge.common.BiomeManager.BiomeEntry;

import com.forgeessentials.multiworld.WorldServerMultiworld;
import com.google.common.collect.ImmutableList;

/**
 * 
 * @author Olee
 */
public class GenLayerMultiworldBiome extends GenLayer
{
    
    protected Random random = new Random();

    @SuppressWarnings("unchecked")
    protected List<BiomeEntry>[] biomes = new ArrayList[BiomeManager.BiomeType.values().length];

    public GenLayerMultiworldBiome(long seed, GenLayer parent, WorldServerMultiworld currentMultiworld)
    {
        super(seed);
        this.parent = parent;

        for (BiomeManager.BiomeType type : BiomeManager.BiomeType.values())
        {
            ImmutableList<BiomeEntry> biomesToAdd = net.minecraftforge.common.BiomeManager.getBiomes(type);
            int idx = type.ordinal();

            if (biomes[idx] == null) biomes[idx] = new java.util.ArrayList<BiomeManager.BiomeEntry>();
            if (biomesToAdd != null) biomes[idx].addAll(biomesToAdd);
        }
        biomes[BiomeManager.BiomeType.DESERT.ordinal()].add(new BiomeEntry(Biomes.PLAINS, 10));
        biomes[BiomeManager.BiomeType.WARM.ordinal()].add(new BiomeEntry(Biomes.PLAINS, 10));
        biomes[BiomeManager.BiomeType.COOL.ordinal()].add(new BiomeEntry(Biomes.PLAINS, 10));
        biomes[BiomeManager.BiomeType.ICY.ordinal()].add(new BiomeEntry(Biomes.PLAINS, 10));
    }

    /**
     * Returns a list of integer values generated by this layer. These may be interpreted as temperatures, rainfall
     * amounts, or biomeList[] indices based on the particular GenLayer subclass.
     */
    @Override
    public int[] getInts(int p_75904_1_, int p_75904_2_, int p_75904_3_, int p_75904_4_)
    {
        int[] aint = this.parent.getInts(p_75904_1_, p_75904_2_, p_75904_3_, p_75904_4_);
        int[] aint1 = IntCache.getIntCache(p_75904_3_ * p_75904_4_);

        for (int i1 = 0; i1 < p_75904_4_; ++i1)
        {
            for (int j1 = 0; j1 < p_75904_3_; ++j1)
            {
                this.initChunkSeed(j1 + p_75904_1_, i1 + p_75904_2_);
                int k1 = aint[j1 + i1 * p_75904_3_];
                int l1 = (k1 & 3840) >> 8;
                k1 &= -3841;

                if (isBiomeOceanic(k1))
                {
                    aint1[j1 + i1 * p_75904_3_] = k1;
                }
                else if (k1 == Biome.REGISTRY.getIDForObject(Biomes.MUSHROOM_ISLAND))
                {
                    aint1[j1 + i1 * p_75904_3_] = k1;
                }
                else if (k1 == 1)
                {
                    if (l1 > 0)
                    {
                        if (this.nextInt(3) == 0)
                        {
                            aint1[j1 + i1 * p_75904_3_] = Biome.REGISTRY.getIDForObject(Biomes.MESA_ROCK);
                        }
                        else
                        {
                            aint1[j1 + i1 * p_75904_3_] = Biome.REGISTRY.getIDForObject(Biomes.MESA_CLEAR_ROCK);
                        }
                    }
                    else
                    {
                        aint1[j1 + i1 * p_75904_3_] = Biome.REGISTRY.getIDForObject(getWeightedBiomeEntry(BiomeManager.BiomeType.DESERT).biome);
                    }
                }
                else if (k1 == 2)
                {
                    if (l1 > 0)
                    {
                        aint1[j1 + i1 * p_75904_3_] = Biome.REGISTRY.getIDForObject(Biomes.JUNGLE);
                    }
                    else
                    {
                        aint1[j1 + i1 * p_75904_3_] = Biome.REGISTRY.getIDForObject(getWeightedBiomeEntry(BiomeManager.BiomeType.WARM).biome);
                    }
                }
                else if (k1 == 3)
                {
                    if (l1 > 0)
                    {
                        aint1[j1 + i1 * p_75904_3_] = Biome.REGISTRY.getIDForObject(Biomes.COLD_TAIGA);
                    }
                    else
                    {
                        aint1[j1 + i1 * p_75904_3_] = Biome.REGISTRY.getIDForObject(getWeightedBiomeEntry(BiomeManager.BiomeType.COOL).biome);
                    }
                }
                else if (k1 == 4)
                {
                    aint1[j1 + i1 * p_75904_3_] = Biome.REGISTRY.getIDForObject(getWeightedBiomeEntry(BiomeManager.BiomeType.ICY).biome);
                }
                else
                {
                    aint1[j1 + i1 * p_75904_3_] = Biome.REGISTRY.getIDForObject(Biomes.MUSHROOM_ISLAND);
                }
            }
        }

        return aint1;
    }

    protected BiomeEntry getWeightedBiomeEntry(BiomeManager.BiomeType type)
    {
        List<BiomeEntry> biomeList = biomes[type.ordinal()];
        int totalWeight = WeightedRandom.getTotalWeight(biomeList);
        int rand = nextInt(totalWeight / 10) * 10;
        int weight = rand + (BiomeManager.isTypeListModded(type) ? nextInt(Math.min(10, totalWeight - rand)) : 0);
        return (BiomeEntry) WeightedRandom.getRandomItem(random, biomeList, weight);
    }
    
}
